﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Text.RegularExpressions;
using System.IO;

namespace AlexTranslator
{
    public class ReplacingPair
    {

        public string To2;

        bool backReplacementOnly;

        public bool BackReplacementOnly
        {
            get { return backReplacementOnly; }
            set { backReplacementOnly = value; }
        }

        string from;

        public string From
        {
            get { return from; }
            set { from = value; }
        }
        string to;

        public string To
        {
            get { return to; }
            set { to = value; }
        }

        

        public ReplacingPair(string from, string to):this(from, to, false)
        {            
        }

        public ReplacingPair(string from, string to, string to2, 
            bool backReplacementOnly)
        {
            From = from;
            To = to;
            To2 = to2;
            BackReplacementOnly = backReplacementOnly;
            
        }
        public ReplacingPair(string from, string to, bool backReplacementOnly
            )
            : this(from, to, "", backReplacementOnly)
        {
        }

        public bool IsSimpleWithSpaces
        {
            get {
                if (to == " " + from + " " )
                    return true;
                else
                    return false;
                }
            
        }

        public virtual string Replace(string src, bool forward)
        {
            ReplacingPair pair = this;
            if (forward)
            {
                if (src.Contains(pair.To) && pair.From != "\n"
                    && !pair.IsSimpleWithSpaces 
                    && !pair.BackReplacementOnly) 
                        MessageBox.Show("Attention: text already contains symbols '" + @pair.To + "'. Formatting could be lost");

                if (!pair.BackReplacementOnly)
                    src = src.Replace(pair.From, pair.To);
            }
            else
            {
                //if (pair.IsTranslation)
                //    continue;

                src = src.Replace(pair.To, pair.From);

                if (!string.IsNullOrEmpty(pair.To2))
                    src = src.Replace(pair.To2, pair.From);

                if (pair.From == "''")
                {
                    src = src.Replace("='' =", pair.From); //google hack
                }

            }
            return src;
        }    
    }

    public class BackReplacingPair : ReplacingPair
    {
        public BackReplacingPair(string from, string to)
            : base(to, from, false)
        {
            BackReplacementOnly = true;
        }
    }

    public class TranslationPair : ReplacingPair
    {
        string translation;

        public TranslationPair(string from, string to, string translation)
            : base(from, to, false)
        {
            this.translation = translation;
        }

        public override string Replace(string src, bool forward)
        {
            TranslationPair pair = this;
            if (forward)
            {
                src = src.Replace(pair.From, pair.To);
                
            }
            else
            {
                src = src.Replace(pair.To, pair.translation);
            }
            return src;
        }
    }

    public class WordsDividerFile : ReplacingPair
    {
        public static string FilePath = AppDomain.CurrentDomain.BaseDirectory + @"Config\CompositeWords.txt";
        List<ReplacingPair> pairs = new List<ReplacingPair>();
        public WordsDividerFile():base("","") { }

        public override string Replace(string src, bool forward)
        {
            if (File.Exists(FilePath))
            {
                if (forward)
                {
                    string[] lines = File.ReadAllLines(FilePath, Encoding.Default);

                    //foreach (string line in lines)
                    for (int i = 0; i < lines.Length; i++)
                    {
                        //string line = lines[i];
                        if (i > 0)
                        {
                            string[] items = lines[i].Split(';'); // akkadsk;0; example
                            pairs.Add(new WordsDivider(items[0].Trim(), false));
                        }
                    }

                    src = TranslationTools.MakeReplacing(src, pairs, forward);
                    
                }
                else
                {
                    src = TranslationTools.MakeReplacing(src, pairs, false);
                    
                }
            }

            return src;
        }
    }

    public class WordsDivider : ReplacingPair
    {
        protected bool InTheBegining {get;set;}
        public WordsDivider(string halfOfWord, bool inTheBegining)
            : base(halfOfWord, "WordsDivider", false)
        {
            this.InTheBegining = inTheBegining;
        }

        public override string Replace(string src, bool forward)
        {
            if (forward)
            {
                //"(\w[^-]*)(slavya\w*)"
                string regex = string.Format(@"(\w[^- |]*)({0}\w*)", From);
                return Regex.Replace(src, regex, "** $1-$2");
            }
            else
            {
                if (false)
                {
                    string regex =
                        //string.Format(@"\*\* {0,4}([\w]{1,15})[-](({0})\w*)",
                        //From);
                        @"\*\* {0,4}([\w]{1,15})[-](\w*)";

                    return Regex.Replace(src, regex, "$1$2");
                }

                return src;
            }
        }
    }

    
}
